package cn.datawin.cmppserver.netway.provider;

import cn.datawin.cmppserver.netway.service.ITcpService;
import cn.datawin.cmppserver.netway.service.impl.CmppAPIServiceImpl;
import cn.datawin.cmppserver.queue.QueueCache;
import cn.datawin.sms.cmpp2.InboxLog;
import cn.datawin.sms.cmpp2.ReportLog;
import cn.datawin.sms.cmpp2.UserReportMsg;
import cn.hutool.extra.spring.SpringUtil;
import com.drondea.sms.channel.ChannelSession;
import com.drondea.sms.common.util.CommonUtil;
import com.drondea.sms.message.ILongSMSMessage;
import com.drondea.sms.message.IMessage;
import com.drondea.sms.message.MessageProvider;
import com.drondea.sms.message.cmpp.CmppDeliverRequestMessage;
import com.drondea.sms.session.AbstractServerSession;
import com.drondea.sms.type.UserChannelConfig;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * 拉取消息的接口实现
 **/
@Slf4j
public class TcpMessageProvider implements MessageProvider {

    private ITcpService tcpService;

    public TcpMessageProvider() {

        this.tcpService = SpringUtil.getBean(CmppAPIServiceImpl.class);
    }

    public String getUserId(ChannelSession channelSession) {
        AbstractServerSession serverSession = (AbstractServerSession) channelSession;
        UserChannelConfig userChannelConfig = serverSession.getUserChannelConfig();
        return userChannelConfig.getId();
    }

    @Override
    public List<IMessage> getTcpMessages(ChannelSession channelSession) {

        String customerCode = getUserId(channelSession);

        List<IMessage> requestMsg = new ArrayList<>();

        //获取回执消息
        pullReportMessageFromMQ(customerCode, channelSession, requestMsg);

        pullMOMessageFromMQ(customerCode, channelSession, requestMsg);

        return requestMsg;
    }

    @Override
    public void responseMessageMatchFailed(String key, IMessage response) {
        log.error("responseMessageMatchFailed {}", response);
    }

    public void pullReportMessageFromMQ(String customerCode, ChannelSession channelSession, List<IMessage> requestMsg) {

        ReportLog report = QueueCache.MESSAGES_REPORT_QUEUE.get(customerCode).poll();
        if (report == null) {
            return;
        }
//        log.debug("mq----ReportLog------{}",report);

        //回执不需要处理长短信
        IMessage reportMessage = tcpService.getReportMessage(channelSession, report);
        if (reportMessage == null) {
            return;
        }
//        log.debug("deal-----ReportLog------{}",reportMessage);
        requestMsg.add(reportMessage);

        CmppDeliverRequestMessage cmppDeliverRequestMessage = (CmppDeliverRequestMessage) reportMessage;
        UserReportMsg userReportMsg = new UserReportMsg();
        userReportMsg.setReportLogId(report.getId());
        userReportMsg.setMsgId(cmppDeliverRequestMessage.getMsgId().toString());
        userReportMsg.setSequence(cmppDeliverRequestMessage.getSequenceId());
        userReportMsg.setYmd(report.getYmd());
//        SpringContextUtil.getBean(ProducerService.class).sendDbMessage(userReportMsg);

    }

    public void pullMOMessageFromMQ(String account, ChannelSession channelSession, List<IMessage> requestMsg) {

        //inbox队列消费
        InboxLog inbox = QueueCache.MESSAGES_INBOX_QUEUE.get(account).poll();
        if (inbox == null) {
            return;
        }
        log.debug("pullMOMessageFromMQ {}", inbox);


        IMessage moMessage = tcpService.getMOMessage(channelSession, inbox);
        try {
            List<IMessage> longMsgToEntity = CommonUtil.getLongMsgSlices((ILongSMSMessage) moMessage,
                    channelSession.getConfiguration(), channelSession.getSequenceNumber());
            requestMsg.addAll(longMsgToEntity);
        } catch (Exception e) {
            log.error("pullMOMessageFromMQ" , e);
        }
    }

}
